package cn.sinata.wanglin.property.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.text.Editable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.TextView;

import com.hyphenate.easeui.domain.EaseEmojicon;
import com.hyphenate.easeui.domain.EaseEmojiconGroupEntity;
import com.hyphenate.easeui.model.EaseDefaultEmojiconDatas;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.hyphenate.easeui.widget.emojicon.EaseEmojiconMenu;
import com.hyphenate.easeui.widget.emojicon.EaseEmojiconMenuBase;
import com.xilada.xldutils.activitys.TitleActivity;
import com.xilada.xldutils.utils.ActivityUtil;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.xilada.xldutils.utils.Utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.Comment;
import cn.sinata.wanglin.model.ProperNotice;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
import cn.sinata.wanglin.village.activity.NewinfoCommentListActivity;

public class ProperNoticeDetailActivity extends TitleActivity {

    @BindView(R.id.mWebView)
    WebView mWebView;
    @BindView(R.id.tv_comment_num)
    TextView tv_comment_num;
    @BindView(R.id.ease_layout_emojicon_menu)
    EaseEmojiconMenu emojiconMenu;
    @BindView(R.id.et_comment)
    EditText et_comment;
    @BindView(R.id.expressionView)
    TextView expressionView;
    @BindView(R.id.tv_action)
    TextView tv_action;
    private String userId;
//    private String id;
    private ProperNotice mProperNotice;
    private boolean isEdit = false;

    @Override
    protected int setContentId() {
        return R.layout.activity_proper_notice_detail;
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("公告详情");

        userId = SharedPreferencesUtils.getString(Constant.SP_USERID);
//        id = getIntent().getStringExtra("id");
        mProperNotice = (ProperNotice) getIntent().getSerializableExtra("data");

        List<EaseEmojiconGroupEntity> emojiconGroupList = new ArrayList<>();
        emojiconGroupList.add(new EaseEmojiconGroupEntity(R.drawable.ee_1,  Arrays.asList(EaseDefaultEmojiconDatas.getData())));
        emojiconMenu.init(emojiconGroupList);
        emojiconMenu.setEmojiconMenuListener(new EaseEmojiconMenuBase.EaseEmojiconMenuListener() {
            @Override
            public void onExpressionClicked(EaseEmojicon emojicon) {
//                et_comment.append(EaseSmileUtils.getSmiledText(mContext,emojicon.getEmojiText()));
                int selection = et_comment.getSelectionEnd();
                et_comment.getText().insert(selection,EaseSmileUtils.getSmiledText(mContext,emojicon.getEmojiText()));
            }

            @Override
            public void onDeleteImageClicked() {
                if (!TextUtils.isEmpty(et_comment.getText())) {
                    KeyEvent event = new KeyEvent(0, 0, 0, KeyEvent.KEYCODE_DEL, 0, 0, 0, 0, KeyEvent.KEYCODE_ENDCALL);
                    et_comment.dispatchKeyEvent(event);
                }
            }
        });

        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                dismissDialog();
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                dismissDialog();
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                if (!TextUtils.isEmpty(url)) {

                    mWebView.loadUrl(url);


                }
                return true;
            }
        });

        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setDefaultTextEncodingName("utf-8");
        if (mProperNotice != null) {
            tv_comment_num.setText(mProperNotice.getCommentCount()+"");
            showDialog();
            mWebView.loadData(mProperNotice.getContent(), "text/html; charset=utf-8", "utf-8");

        }

    }

    @OnTextChanged(R.id.et_comment)
    void onCommentChanged(Editable s){
        if (TextUtils.isEmpty(s)) {
            tv_action.setVisibility(View.GONE);
            tv_comment_num.setVisibility(View.VISIBLE);
        } else {
            tv_action.setVisibility(View.VISIBLE);
            tv_comment_num.setVisibility(View.GONE);
        }
    }

    void addComment(){
        String content = et_comment.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            showToast("发送文字不能为空");
            return;
        }
        showDialog();
        String id = "";
        if (mProperNotice != null) {
            id = mProperNotice.getId();
        }
        HttpManager.addComment(userId,"E",id,null,content,0,"0",null)
                .subscribe(new ResultDataSubscriber<Comment>(this) {
                    @Override
                    public void onSuccess(String msg, Comment comment) {
                        showToast(msg);
                        Utils.hideSoftWindow(mContext,et_comment);
                        int count = mProperNotice.getCommentCount();
                        mProperNotice.setCommentCount(count+1);
                        tv_comment_num.setText(mProperNotice.getCommentCount()+"");
                        isEdit = true;
                        emojiconMenu.setVisibility(View.GONE);
                        et_comment.setText("");
                        et_comment.clearFocus();
                        setResult(RESULT_OK);
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 10) {
                int count = mProperNotice.getCommentCount();
                mProperNotice.setCommentCount(count+1);
                tv_comment_num.setText(mProperNotice.getCommentCount()+"");
                isEdit = true;
            }
        }
    }

    @Override
    public void finish() {
        if (isEdit) {
            setResult(RESULT_OK);
        }
        super.finish();
    }


    @OnClick({R.id.expressionView,R.id.et_comment,R.id.tv_action,R.id.tv_comment_num})
    void onViewClick(View v) {
        switch (v.getId()) {
            case R.id.expressionView:

                Utils.hideSoftWindow(this,v);
                if (emojiconMenu.getVisibility() == View.VISIBLE) {
                    emojiconMenu.setVisibility(View.GONE);
                } else {
                    emojiconMenu.setVisibility(View.VISIBLE);
                    et_comment.requestFocus();
                }

                break;
            case R.id.et_comment:
                emojiconMenu.setVisibility(View.GONE);
                break;
            case R.id.tv_action:
                addComment();
                break;
            case R.id.tv_comment_num:
                String id = "";
                if (mProperNotice != null) {
                    id = mProperNotice.getId();
                }
                if(TextUtils.isEmpty(id)){
                    return;
                }
                ActivityUtil.create(mContext).go(NewinfoCommentListActivity.class).put("id",id)
                        .put("type","E")
                        .startForResult(10);
                break;

        }
    }
}
